package com.ljm.boot.helloword.letCode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;


//letCode 两数之和
public class Test01 {

    public static void main(String[] args) {
        //test(new int[]{1,2,3},5);
        letCode(new int[]{1,2,3},5);
    }

    //letCode
    public static int[]  letCode(int[] nums ,int target){
        int[] arr = new int[2];
        Map<Integer,Integer> map =  new HashMap<>();
        for (int i=0 ;i< nums.length;i++) {
            //int key = target-nums[i];
            if(map.containsKey(nums[i])){
                arr[0] = map.get(nums[i]);
                arr[1] = i;
            }else{
                map.put(target-nums[i],i);
            }

        }
        System.out.println(Arrays.toString(arr));
        return arr;
    }

    public static int[]  test(int[] nums ,int target){
        int[] arr = new int[2];
        int count  =  nums.length;
        for (int i=0 ;i<count;i++) {
            for (int j=i+1 ;j<count;j++) {
                if(nums[i]+nums[j]==target){
                    arr = new int[]{i,j};
                    break;
                }
            }
        }
        System.out.println(Arrays.toString(arr));

        return arr;
    }
}
